﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using MAVI.BillRecognition.BillDetection.source;

namespace MAVI
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string recognizeImage(Byte[] imageBytes, String s)
        {
            System.IO.MemoryStream ms = new MemoryStream(imageBytes);

            Bitmap b = (Bitmap)Image.FromStream(ms);

            if (s.Equals("bill recognition"))
            {
                //call function for bill recog.

                RectangleDetection test = new RectangleDetection();

                test.Detect(b);

            }

            if (s.Equals("sticker recognition"))
            {
                //call function for sticker recog.
            }

            return "Image read";
        }

        // save image to Images and sound to Sounds
        // give name random
        [WebMethod]
        public string tagImage(Byte[] imageBytes, Byte[] soundBytes)
        {
            String name = "randomName";

            MemoryStream ms = new MemoryStream(imageBytes);
            MemoryStream ms2 = new MemoryStream(soundBytes);

            Bitmap b = (Bitmap)Image.FromStream(ms);
            b.Save("D:\\ImagineCup\\Images\\" + name + ".jpg", ImageFormat.Jpeg);


            ms2.Seek(0, SeekOrigin.Begin);
            FileStream fs = File.Create("D:\\ImagineCup\\Sounds\\" + name + ".wav");
            byte[] buf = new byte[65536];
            int len = 0;
            while ((len = ms2.Read(buf, 0, 65536)) > 0)
            {
                fs.Write(buf, 0, len);
            }
            fs.Close();

            return "Image taged";
        }
    }
}